package com.wangyongliang.personalcenterproducer.service.impl;

import ch.qos.logback.core.util.TimeUtil;
import com.wangyongliang.common.dto.UserDto;
import com.wangyongliang.personalcenterproducer.entity.User;
import com.wangyongliang.personalcenterproducer.mapper.UserMapper;
import com.wangyongliang.personalcenterproducer.service.IUserService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.wangyongliang.personalcenterproducer.utils.PhoneCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 王永亮
 * @since 2020-03-24
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    RedisTemplate redisTemplate;


    @Override
    public boolean getCode(String phone) {
        String phonemsg = PhoneCode.getPhonemsg(phone);

        if (StringUtils.isNotBlank(phonemsg)) {
            redisTemplate.opsForValue().set(phone,phonemsg,3600, TimeUnit.SECONDS);
            return true;
        }

        return false;
    }

    @Override
    public String codeinfo(UserDto userDto) {
        String codeIn = (String) redisTemplate.opsForValue().get(userDto.getPhone());
        log.info("redis中的验证码------>{}",codeIn);


        if (!userDto.getCode().equals(codeIn)){
            log.info(userDto.getCode());
            return "验证码错误！";
        }
        if (!userDto.getPassword().equals(userDto.getPasswordcopy())){
            return "两次输入的密码不同";
        }

        Integer update = this.baseMapper.updatePhoen(userDto.getPhone(),userDto.getUid());

        return "1";
    }
}
